(function($) {
	var $this = null;

	$(function() {
		$("#vouchersListPanel_close").bind("click", closeShopListPanel);
		// 关闭
		$("#vouchersListPanel_search").bind("click", search);
		// 查询
	});
	/**
	 * 显示商铺列表层
	 */
	function openvouchersListPanel(self) {
		$this = $(self);
		$this.blur();
		$("#vouchersListPanel_searchName").val('');
		search();
	}

	/**
	 * 加载数据
	 */
	function search() {
		var content = $("#vouchersListPanel_searchName").val();
		new Pagination({
			method : 'get', // 请求方法 post|get
			type : 'json', // 返回内容格式，json|text //NOT SUPPORT
			url : '../vouchersType/find/vouchersTypeList', // 请求地址，注意函数会自定添加分页的两个参数
			// ‘0/10’
			data : {
				content : content
			}, // 使用post 方法是的请求参数
			pageSize : 10, // 页面大小
			startIndex : 0, // 服务器提供的接口页码是从几开始的，建议从0开始
			success : printDataToPage, // state 为 0 时，回调
			pagerHandler : 'vouchersPager', // 显示分页的 div 的 id
			last : last
		});
	}

	function last(message) {
		$("#vouchersListPanel,#vouchersListPanel_yinying").show();
		addMargin($("#vouchersListPanel"));
	}

	function printDataToPage(message) {
		var $tbody = $("#vouchersListPanel_data");
		$tbody.empty();
		// 清空数据
		var len = message.result.data.length;
		// 返回条目数
		for (var i = 0; i < len; i++) {
			// "goodsId","goodsName","barCode"
			var goodsId = getHtmlString(getHeadIndexValue(i, message, 'id'));
			var goodsName = getHtmlString(getHeadIndexValue(i, message, 'name'));

			var select = "<td><input class=\"common_btn\" type=\"button\" onclick=\"__vouchersSelected('" + goodsId + "','" + goodsName + "')\" value=\"选择\"></input></td>";

			var tr = "<tr>" + "<td>" + goodsId + "</td>" + "<td>" + goodsName + "</td>" + select + "</tr>";
			$tbody.append($(tr));
		}
	}

	/**
	 * 关闭
	 */
	function closeShopListPanel() {
		$("#vouchersListPanel,#vouchersListPanel_yinying").hide();
	}

	/**
	 * 选择并关闭
	 */
	function __vouchersSelected(goodsId, goodsName) {
		$this.empty();
		$this.append("<option value='" + goodsId + "' selected=\"selected\" >" + goodsName + "</option>");
		$("#vouchersListPanel,#vouchersListPanel_yinying").hide();

		if (window.vouchersSelected) {
			window.vouchersSelected(goodsId, goodsName);
		}
	}


	window.__vouchersSelected = __vouchersSelected;
	window.openvouchersListPanel = openvouchersListPanel;
})(jQuery);
